package com.cn.xact.ty_treasured_book.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cn.xact.ty_treasured_book.R;
import com.cn.xact.ty_treasured_book.net.NetworkCallBack;
import com.cn.xact.ty_treasured_book.net.NetworkImpl;
import com.cn.xact.ty_treasured_book.util.UIHelper;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class HomeCreditExchangeListActivity extends Activity {

    private List<Map<String, Object>> mListItems;
    private ListView mExchangeList;
    private HomeCreditExchangeListViewAdapter mExchangeAdapter;
    private String mCostPre;
    private NetworkImpl mHomeExchangeListNetwork;

    private String[] mExchangeTitle;
    private String[] mEchangeCost;
    private Bitmap[] mExchangeIcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_home_credit_exchange_list);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_customer_title);

        ImageView tmp = (ImageView)findViewById(R.id.common_title_indicator);
        tmp.setImageResource(R.drawable.main_home);
        TextView title1 = (TextView)findViewById(R.id.common_title_first_section);
        title1.setText(R.string.title_activity_home);
        TextView separator1 = (TextView)findViewById(R.id.common_title_first_separator);
        separator1.setVisibility(View.VISIBLE);
        TextView title2 = (TextView)findViewById(R.id.common_title_second_section);
        title2.setText(R.string.home_user_exchange);
        title2.setVisibility(View.VISIBLE);

        title1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                HomeCreditExchangeListActivity.this.finish();
            }

        });

        mHomeExchangeListNetwork = new NetworkImpl(this, new MyNetworkCallBack());
        mHomeExchangeListNetwork.getExchangeListAsync(123);

        mExchangeList = (ListView)findViewById(R.id.home_user_credit_exchange_list);
        mListItems = getListItems();
        mExchangeAdapter = new HomeCreditExchangeListViewAdapter(this, mListItems);
        mExchangeList.setAdapter(mExchangeAdapter);

        mCostPre = getResources().getString(R.string.home_user_credit_exchange_cost);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home_credit_exchange_list, menu);
        return true;
    }

    private List<Map<String, Object>> getListItems() {
        List<Map<String, Object>> innnerListItems = new ArrayList<Map<String, Object>>();
        if (mExchangeTitle != null) {
            for(int i = 0; i < mExchangeTitle.length; i++) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("icon", mExchangeIcon[i]);
                map.put("title", mExchangeTitle[i]);
                map.put("cost", mEchangeCost[i]);
                innnerListItems.add(map);
            }
        }
        return innnerListItems;
    }

    private class HomeCreditExchangeListViewAdapter extends BaseAdapter {

        private List<Map<String, Object>> listItems;
        private LayoutInflater listContainer;

        public final class ListItemView{
            public ImageView icon;
            public TextView title;
            public TextView cost;
        }

        public HomeCreditExchangeListViewAdapter(Context context, List<Map<String, Object>> listItems) {
            listContainer = LayoutInflater.from(context);
            this.listItems = listItems;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return listItems.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return listItems.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ListItemView  listItemView = null;
            if (convertView == null) {
                listItemView = new ListItemView();
                convertView = listContainer.inflate(R.layout.home_credit_exhange_item, null);

                listItemView.icon = (ImageView)convertView.findViewById(R.id.home_credit_exchange_icon);
                listItemView.title = (TextView)convertView.findViewById(R.id.home_credit_exchange_title);
                listItemView.cost = (TextView)convertView.findViewById(R.id.home_credit_exchange_cost);
                convertView.setTag(listItemView);
            }else {
                listItemView = (ListItemView)convertView.getTag();
            }

            final String title = (String)listItems.get(position).get("title");
            final String cost  = (String)listItems.get(position).get("cost");
            final Bitmap icon = (Bitmap)listItems.get(position).get("icon");

            listItemView.title.setText(title);
            listItemView.cost.setText(mCostPre + cost);
            listItemView.icon.setImageBitmap(icon);

            Button exchangeConfirmBt = (Button)convertView.findViewById(R.id.home_credit_exchange_button);
            exchangeConfirmBt.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Intent tmp = new Intent(
                              HomeCreditExchangeListActivity.this
                            , HomeCreditExchangeActivity.class);

                    tmp.putExtra("title", title);
                    tmp.putExtra("cost", cost);
                    startActivity(tmp);
                    HomeCreditExchangeListActivity.this.finish();
                }

            });

            return convertView;
        }

    }

    class MyNetworkCallBack extends NetworkCallBack {
        @SuppressWarnings("unchecked")
        public void onNetworkCallBack(int id, boolean success, Object attach1, Object attach2) {
            if (!success) {
                UIHelper.showConfirmDialog(HomeCreditExchangeListActivity.this, null, getResources().getString(R.string.server_busy));
                return;
            }
            switch(id) {
            case NetworkImpl.NET_WORK_ID_GET_EXCHANGE_LIST:
                mListItems = (List<Map<String, Object>>)attach1;
                mExchangeAdapter = new HomeCreditExchangeListViewAdapter(HomeCreditExchangeListActivity.this, mListItems);
                mExchangeList.setAdapter(mExchangeAdapter);
                break;
            }
        }
    }

}
